//
//  RegisterViewController.m
//  SlothSecondProj
//
//  Created by 焱 孙 on 14-4-1.
//  Copyright (c) 2014年 visionet. All rights reserved.
//

#import "RegisterViewController.h"
#import "UserVo.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dicOffsetY = [[NSMutableDictionary alloc]init];
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initView
{
    [self setTopNavBarTitle:[Common localStr:@"Login_Activation" value:@"激　活"]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.m_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,NAV_BAR_HEIGHT,kScreenWidth,kScreenHeight-NAV_BAR_HEIGHT)];
    self.m_scrollView.backgroundColor = [UIColor clearColor];
    self.m_scrollView.autoresizingMask = NO;
    self.m_scrollView.clipsToBounds = YES;
    [self.view addSubview:self.m_scrollView];
    
    CGFloat fHeight = 10.0;
    //必填项
    self.lblMustCompleteOption = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 200, 17)];
    self.lblMustCompleteOption.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
    self.lblMustCompleteOption.textColor = COLOR(51, 51, 51, 1.0);
    self.lblMustCompleteOption.text = [Common localStr:@"Login_ActivationRequire" value:@"必填项"];
    [self.m_scrollView addSubview:self.lblMustCompleteOption];
    fHeight += 41;
    
    self.imgViewTopBK = [[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight, kScreenWidth, 5*41)];
    self.imgViewTopBK.image = [[UIImage imageNamed:@"groupedit_cell_bk"] stretchableImageWithLeftCapWidth:160 topCapHeight:20];
    [self.m_scrollView addSubview:self.imgViewTopBK];
    
    //1.phone num
    self.lblPhoneNum = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblPhoneNum.text = [Common localStr:@"UserGroup_PhoneNumber" value:@"手机"];
    self.lblPhoneNum.font = [UIFont fontWithName:@"Helvetica" size:16.0];
    self.lblPhoneNum.textColor = COLOR(51, 51, 51, 1.0);
    self.lblPhoneNum.textAlignment = NSTextAlignmentLeft;
    self.lblPhoneNum.backgroundColor = [UIColor clearColor];
    [self.m_scrollView addSubview:self.lblPhoneNum];
    
    self.txtPhoneNum = [[UITextField alloc]initWithFrame:CGRectMake(90,fHeight+12,kScreenWidth-110,17)];
    self.txtPhoneNum.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtPhoneNum.textColor = COLOR(51, 51, 51, 1.0);
    self.txtPhoneNum.placeholder = [Common localStr:@"UserGroup_PhoneNumber" value:@"手机"];
    self.txtPhoneNum.delegate = self;
    self.txtPhoneNum.tag = 501;
    self.txtPhoneNum.keyboardType = UIKeyboardTypeNamePhonePad;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"501"];
    [self.m_scrollView addSubview:self.txtPhoneNum];
    
    UIView *viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, kScreenWidth-20, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    fHeight += 41;
    
    //2.password
    self.lblPwd = [[UILabel alloc]initWithFrame:CGRectMake(20,fHeight+12,65,(17))];
    self.lblPwd.font = [UIFont fontWithName:APP_FONT_NAME size:(16)];
    self.lblPwd.textColor = COLOR(51, 51, 51, 1.0);
    self.lblPwd.text = [Common localStr:@"Login_ActivationPwd" value:@"密码"];
    self.lblPwd.textAlignment = NSTextAlignmentLeft;
    self.lblPwd.backgroundColor = [UIColor clearColor];
    [self.m_scrollView addSubview:self.lblPwd];
    
    self.txtPwd = [[UITextField alloc]initWithFrame:CGRectMake(90, fHeight+12,kScreenWidth-110, (17))];
    self.txtPwd.font = [UIFont fontWithName:APP_FONT_NAME size:(16)];
    self.txtPwd.textColor = COLOR(51, 51, 51, 1.0);
    self.txtPwd.secureTextEntry = YES;
    self.txtPwd.placeholder = [Common localStr:@"Login_ActivationPwd" value:@"密码"];
    self.txtPwd.delegate = self;
    self.txtPwd.tag = 502;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"502"];
    [self.m_scrollView addSubview:self.txtPwd];
    
    viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, kScreenWidth-20, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    fHeight += 41;
    
    //3.repeat password
    self.lblRepeatPwd = [[UILabel alloc]initWithFrame:CGRectMake(20,fHeight+12,65,(17))];
    self.lblRepeatPwd.font = [UIFont fontWithName:APP_FONT_NAME size:(16)];
    self.lblRepeatPwd.textColor = COLOR(51, 51, 51, 1.0);
    self.lblRepeatPwd.text = [Common localStr:@"Login_RepeatPwd" value:@"重复密码"];
    self.lblRepeatPwd.textAlignment = NSTextAlignmentLeft;
    self.lblRepeatPwd.backgroundColor = [UIColor clearColor];
    [self.m_scrollView addSubview:self.lblRepeatPwd];
    
    self.txtRepeatPwd = [[UITextField alloc]initWithFrame:CGRectMake(90, fHeight+12,kScreenWidth-110, (17))];
    self.txtRepeatPwd.font = [UIFont fontWithName:APP_FONT_NAME size:(16)];
    self.txtRepeatPwd.textColor = COLOR(51, 51, 51, 1.0);
    self.txtRepeatPwd.secureTextEntry = YES;
    self.txtRepeatPwd.placeholder = [Common localStr:@"Login_RepeatPwd" value:@"重复密码"];
    self.txtRepeatPwd.delegate = self;
    self.txtRepeatPwd.tag = 503;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"503"];
    [self.m_scrollView addSubview:self.txtRepeatPwd];
    
    viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, kScreenWidth-20, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    fHeight += 41;
    
    //4.aligns name
    self.lblName = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblName.text = [Common localStr:@"UserGroup_Nickname" value:@"昵称"];
    self.lblName.font = [UIFont fontWithName:@"Helvetica" size:16.0];
    self.lblName.textColor = COLOR(51, 51, 51, 1.0);
    self.lblName.textAlignment = NSTextAlignmentLeft;
    self.lblName.backgroundColor = [UIColor clearColor];
    [self.m_scrollView addSubview:self.lblName];
    
    self.txtName = [[UITextField alloc]initWithFrame:CGRectMake(90,fHeight+12, kScreenWidth-110,17)];
    self.txtName.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtName.textColor = COLOR(51, 51, 51, 1.0);
    self.txtName.placeholder = [Common localStr:@"UserGroup_Nickname" value:@"昵称"];
    self.txtName.delegate = self;
    self.txtName.tag = 504;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"504"];
    [self.m_scrollView addSubview:self.txtName];
    
    viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, kScreenWidth-20, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    fHeight += 41;
    
    //5.identifying code
    self.lblIdentifyingCode = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblIdentifyingCode.text = [Common localStr:@"Login_VerifyCode" value:@"验 证 码"];
    self.lblIdentifyingCode.font = [UIFont fontWithName:@"Helvetica" size:16.0];
    self.lblIdentifyingCode.textColor = COLOR(51, 51, 51, 1.0);
    self.lblIdentifyingCode.textAlignment = NSTextAlignmentLeft;
    self.lblIdentifyingCode.backgroundColor = [UIColor clearColor];
    [self.m_scrollView addSubview:self.lblIdentifyingCode];
    
    self.txtIdentifyingCode = [[UITextField alloc]initWithFrame:CGRectMake(90,fHeight+12, 120,17)];
    self.txtIdentifyingCode.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtIdentifyingCode.textColor = COLOR(51, 51, 51, 1.0);
    self.txtIdentifyingCode.placeholder = [Common localStr:@"Login_VerifyCode" value:@"验 证 码"];
    self.txtIdentifyingCode.delegate = self;
    self.txtIdentifyingCode.tag = 505;
    self.txtIdentifyingCode.keyboardType = UIKeyboardTypeNamePhonePad;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"505"];
    [self.m_scrollView addSubview:self.txtIdentifyingCode];
    
    self.btnCode = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnCode setTitleColor:COLOR(255, 255, 255, 0.5) forState:UIControlStateHighlighted];
    [self.btnCode.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15.0]];
    [self.btnCode setBackgroundImage:[Common getImageWithColor:COLOR(91, 192, 222, 1.0)] forState:UIControlStateNormal];
    [self.btnCode setTitle:[Common localStr:@"Login_GetVerifyCode" value:@"获取验证码"] forState:UIControlStateNormal];
    self.btnCode.frame = CGRectMake(kScreenWidth-105, fHeight+5.5, 90, 30);
    
    [self.btnCode.layer setBorderWidth:1.0];
    [self.btnCode.layer setCornerRadius:3];
    self.btnCode.layer.borderColor = [[UIColor clearColor] CGColor];
    [self.btnCode.layer setMasksToBounds:YES];
    
    [self.btnCode addTarget:self action:@selector(getIdentifyingCode) forControlEvents:UIControlEventTouchUpInside];
    [self.m_scrollView addSubview:self.btnCode];
    
    fHeight += 41;
    
    //选填项
    self.lblCouldCompleteOption = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 150, 17)];
    self.lblCouldCompleteOption.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
    self.lblCouldCompleteOption.textColor = COLOR(51, 51, 51, 1.0);
    self.lblCouldCompleteOption.text = [Common localStr:@"Login_ActivationOptional" value:@"选填项"];
    [self.m_scrollView addSubview:self.lblCouldCompleteOption];
    fHeight += 41;
    
    self.imgViewBottomBK = [[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight, kScreenWidth, 3*41)];
    self.imgViewBottomBK.image = [[UIImage imageNamed:@"groupedit_cell_bk"] stretchableImageWithLeftCapWidth:160 topCapHeight:20];
    [self.m_scrollView addSubview:self.imgViewBottomBK];
    
    //1.birthday
    self.lblBirthday = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblBirthday.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.lblBirthday.textColor = COLOR(51, 51, 51, 1.0);
    self.lblBirthday.text = [Common localStr:@"UserGroup_DateOfBirth" value:@"生日"];
    [self.m_scrollView addSubview:self.lblBirthday];
    
    self.txtBirthday = [[UITextField alloc]initWithFrame:CGRectMake(90, fHeight+12, kScreenWidth-110,17)];
    self.txtBirthday.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtBirthday.textColor = COLOR(51, 51, 51, 1.0);
    self.txtBirthday.placeholder = [Common localStr:@"UserGroup_DateOfBirth" value:@"生日"];
    self.txtBirthday.delegate = self;
    self.txtBirthday.tag = 506;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"506"];
    [self.m_scrollView addSubview:self.txtBirthday];
    
    self.btnBirthday = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnBirthday addTarget:self action:@selector(doSelectDate:) forControlEvents:UIControlEventTouchUpInside];
    self.btnBirthday.frame = CGRectMake(11, fHeight+2, 298, 41-4);
    [self.btnBirthday setBackgroundImage:[Common getImageWithColor:COLOR(150, 150, 150, 0.4)] forState:UIControlStateHighlighted];
    [self.m_scrollView addSubview:self.btnBirthday];
    
    viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, 300, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    
    self.pickerDate = [[CustomDatePicker alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260) andDelegate:self];
    self.pickerDate.delegate = self;
    [self.view addSubview:self.pickerDate];
    fHeight += 41;
    
    //2.address
    self.lblAddress = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblAddress.text = [Common localStr:@"UserGroup_Address" value:@"地址"];
    self.lblAddress.font = [UIFont fontWithName:@"Helvetica" size:16.0];
    self.lblAddress.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:self.lblAddress];
    
    self.txtAddress = [[UITextField alloc]initWithFrame:CGRectMake(90,fHeight+12, kScreenWidth-110,17)];
    self.txtAddress.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtAddress.textColor = COLOR(51, 51, 51, 1.0);
    self.txtAddress.placeholder = [Common localStr:@"UserGroup_Address" value:@"地址"];
    self.txtAddress.delegate = self;
    self.txtAddress.tag = 507;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"507"];
    [self.m_scrollView addSubview:self.txtAddress];
    
    viewLine = [[UIView alloc]initWithFrame:CGRectMake(10, fHeight+40.5, 300, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [self.m_scrollView addSubview:viewLine];
    
    fHeight += 41;
    
    //3.email
    self.lblEmail = [[UILabel alloc]initWithFrame:CGRectMake(20, fHeight+12, 65, 17)];
    self.lblEmail.text = [Common localStr:@"UserGroup_Email" value:@"邮箱"];
    self.lblEmail.font = [UIFont fontWithName:@"Helvetica" size:16.0];
    self.lblEmail.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:self.lblEmail];
    
    self.txtEmail = [[UITextField alloc]initWithFrame:CGRectMake(90,fHeight+12, kScreenWidth-110,17)];
    self.txtEmail.font = [UIFont fontWithName:APP_FONT_NAME size:16];
    self.txtEmail.textColor = COLOR(51, 51, 51, 1.0);
    self.txtEmail.placeholder = [Common localStr:@"UserGroup_Email" value:@"邮箱"];
    self.txtEmail.delegate = self;
    self.txtEmail.keyboardType = UIKeyboardTypeEmailAddress;
    self.txtEmail.tag = 508;
    [self.dicOffsetY setObject:[NSNumber numberWithFloat:fHeight+15] forKey:@"508"];
    [self.m_scrollView addSubview:self.txtEmail];
    fHeight += 41;
    
    //4.register button
    self.btnRegister = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnRegister setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnRegister setTitleColor:COLOR(255, 255, 255, 0.5) forState:UIControlStateHighlighted];
    [self.btnRegister.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];
    [self.btnRegister setBackgroundImage:[Common getImageWithColor:COLOR(91, 192, 222, 1.0)] forState:UIControlStateNormal];
    [self.btnRegister setTitle:[Common localStr:@"Login_Activation" value:@"激　活"] forState:UIControlStateNormal];
    self.btnRegister.frame = CGRectMake(40, fHeight+12, kScreenWidth-80, 30);
    
    [self.btnRegister.layer setBorderWidth:1.0];
    [self.btnRegister.layer setCornerRadius:3];
    self.btnRegister.layer.borderColor = [[UIColor clearColor] CGColor];
    [self.btnRegister.layer setMasksToBounds:YES];
    
    [self.btnRegister addTarget:self action:@selector(doRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.m_scrollView addSubview:self.btnRegister];
    
    fHeight += 12+30+10;
    
    fHeight += 255;
    [self.m_scrollView setContentSize:CGSizeMake(kScreenWidth, fHeight)];
}

//选择日期
-(void)doSelectDate:(UIButton*)sender
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (sender == self.btnBirthday)
    {
        //出生日期
        [self fieldOffset:self.txtBirthday.tag];
        [self setPickerHidden:self.pickerDate andHide:NO];
        NSString *strDate = self.txtBirthday.text;
        if (strDate.length>0)
        {
            NSDate *date = [Common getDateFromDateStr:strDate];
            if (date != nil)
            {
                [self.pickerDate.pickerView setDate:date animated:YES];
            }
        }
    }
}

//注册
-(void)doRegister
{
    UserVo *userVo = [[UserVo alloc]init];
    //1.登录名
    userVo.strLoginAccount = self.txtPhoneNum.text;
    if (userVo.strLoginAccount == nil || userVo.strLoginAccount.length==0)
    {
        [Common tipAlert:[Common localStr:@"Login_PhoneTip" value:@"请输入手机号"]];
        return;
    }
    
    //手机号	事先由管理员导入，需验证
    userVo.strPhoneNumber = self.txtPhoneNum.text;
    
    //2.密码	明文
    userVo.strPassword = self.txtPwd.text;
    if (userVo.strPassword == nil || userVo.strPassword.length==0)
    {
        [Common tipAlert:[Common localStr:@"Login_ActivationEnterPwd" value:@"请输入密码"]];
        return;
    }
    NSString *strRepeatPwd = self.txtRepeatPwd.text;
    if (strRepeatPwd == nil || strRepeatPwd.length==0)
    {
        [Common tipAlert:[Common localStr:@"Login_RepeatPwdTip" value:@"请输入重复密码"]];
        return;
    }
    if (![userVo.strPassword isEqualToString:strRepeatPwd])
    {
        [Common tipAlert:[Common localStr:@"Login_ActivationCheckPwd" value:@"密码与重复密码不一致"]];
        return;
    }
    
    //3.昵称
    userVo.strUserName = self.txtName.text;
    if (userVo.strUserName == nil || userVo.strUserName.length==0)
    {
        [Common tipAlert:[Common localStr:@"Login_ActivationCheckNickname" value:@"请输入昵称"]];
        return;
    }
    
    //4.验证码	发送到对应手机
    userVo.strIdentifyCode = self.txtIdentifyingCode.text;
    if (userVo.strIdentifyCode == nil || userVo.strIdentifyCode.length==0)
    {
        [Common tipAlert:[Common localStr:@"Login_VerifyCodeTip" value:@"请输入验证码"]];
        return;
    }
    
    //5.生日	yyyy-MM-dd
    userVo.strBirthday = self.txtBirthday.text;
    
    //6.职位
    userVo.strAddress = self.txtAddress.text;
    
    //7.电子邮件
    userVo.strEmail = self.txtEmail.text;
    
    //8.所在地
    userVo.strAddress = self.txtAddress.text;
    
    //9.个人网站
    
//    [self isHideActivity:NO];
//    dispatch_async(dispatch_get_global_queue(0,0),^{
//        //do thread work
//    	ServerReturnInfo *retInfo = [ServerProvider registerUserAction:userVo];
//    	if (retInfo.bSuccess)
//    	{
//        	dispatch_async(dispatch_get_main_queue(), ^{
//                [self isHideActivity:YES];
//                [self backButtonClicked];
//        	});
//    	}
//    	else
//    	{
//        	dispatch_async(dispatch_get_main_queue(), ^{
//                [self isHideActivity:YES];
//                [Common tipAlert:retInfo.strErrorMsg];
//        	});
//    	}
//    });
}

//
-(void)getIdentifyingCode
{
    NSString *strPhoneNum = self.txtPhoneNum.text;
    if (strPhoneNum == nil || strPhoneNum.length == 0)
    {
        [Common tipAlert:[Common localStr:@"Login_PhoneTip" value:@"请输入手机号码"]];
        return;
    }
    
//    [self isHideActivity:NO];
//    dispatch_async(dispatch_get_global_queue(0,0),^{
//        //do thread work
//    	ServerReturnInfo *retInfo = [ServerProvider sendIdentifyingCode:strPhoneNum];
//    	if (retInfo.bSuccess)
//    	{
//        	dispatch_async(dispatch_get_main_queue(), ^{
//                [self isHideActivity:YES];
//        	});
//    	}
//    	else
//    	{
//        	dispatch_async(dispatch_get_main_queue(), ^{
//                [Common tipAlert:retInfo.strErrorMsg];
//                [self isHideActivity:YES];
//        	});
//    	}
//    });
}

//调整键盘高度
-(void)fieldOffset:(NSUInteger)nTextTag
{
    //获取特定的UITextField偏移量
	float offset = [[self.dicOffsetY objectForKey:[NSString stringWithFormat:@"%lu",(unsigned long)nTextTag]] floatValue];
    //当前scrollView 偏移量
	float scrollOffset = self.m_scrollView.contentOffset.y;
    
    //键盘标准高度值
    float fHeighKey = kScreenHeight-NAV_BAR_HEIGHT-252;//中文键盘252
    //控件当前高度值（本身高度-scrollview滚动高度）
    float fCtrlHeight = offset-scrollOffset;
	
	if (fCtrlHeight>fHeighKey)
    {
        //被键盘遮挡，需要滚动到最上方
		[self.m_scrollView setContentOffset:CGPointMake(0, (offset-80)) animated:YES];//offset-10
	}
}

//隐藏键盘
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
	UITouch*touch =[touches anyObject];
	if(touch.phase==UITouchPhaseBegan)
    {
		//find first response view
		for(UIView*view in[self.m_scrollView subviews])
        {
			if([view isFirstResponder])
            {
				[view resignFirstResponder];
				break;
			}
		}
	}
}

//显示隐藏时间控件
- (void)setPickerHidden:(UIView*)pickerViewCtrl andHide:(BOOL)hidden
{
    int nHeight = pickerViewCtrl.frame.size.height * (-1);
    CGAffineTransform transform = hidden ? CGAffineTransformIdentity : CGAffineTransformMakeTranslation(0, nHeight);
    pickerViewCtrl.transform = transform;
    if (!hidden)
    {
        self.view.backgroundColor = [UIColor whiteColor];
        self.m_scrollView.frame = CGRectMake(0, NAV_BAR_HEIGHT, self.view.bounds.size.width, (kScreenHeight-NAV_BAR_HEIGHT-260+44));
    }
    else
    {
        self.m_scrollView.frame = CGRectMake(0, NAV_BAR_HEIGHT, self.view.bounds.size.width, kScreenHeight-NAV_BAR_HEIGHT);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self setPickerHidden:self.pickerDate andHide:YES];
    //编辑时调整view
    [self fieldOffset:textField.tag];
}

#pragma mark - CustomDatePickerDelegate
- (void)doneDatePickerButtonClick:(CustomDatePicker*)pickerViewCtrl
{
    [self setPickerHidden:pickerViewCtrl andHide:YES];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    if (pickerViewCtrl == self.pickerDate)
    {
        NSDate *date = pickerViewCtrl.pickerView.date;
        self.txtBirthday.text = [dateFormatter stringFromDate:date];
    }
}

- (void)cancelDatePickerButtonClick:(CustomDatePicker*)pickerViewCtrl
{
    [self setPickerHidden:pickerViewCtrl andHide:YES];
}

@end
